package com.dk.controller;

import com.dk.pojo.TabUser;
import com.dk.service.RegisterService;
import com.dk.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;

@Controller
@RequestMapping("registerController")
public class RegisterController {
    @Autowired
    RegisterService registerService;

    @RequestMapping("isUserNameExists")
    @ResponseBody
    public Integer isUserNameExists(String username){

        int i = registerService.isUserNameExists(username);
        return i;
    }
    @RequestMapping("register")
    @ResponseBody
    public Integer register(String check,TabUser user, HttpServletRequest request){
        HttpSession session = request.getSession();
        String checkCode = (String) session.getAttribute("check_code");
        if (check!=null&&check.equalsIgnoreCase(checkCode)){
            try {
                String encryptedPwd = MD5Utils.getEncryptedPwd(user.getPassword());
                user.setPassword(encryptedPwd);
                int i = registerService.saveUser(user);
                return i;
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }

        }
        return 0;
    }
}
